//
//  MainTabbarController.m
//  QRBuildScan
//
//  Created by  on 12-1-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MainTabbarController.h"
#import "ScanViewController.h"
#import "HistoryController.h"
#import "ComController.h"
#import "LoginController.h"
static UIView*  p_CustomTabbar;
@implementation MainTabbarController
-(id) init{
    if ((self = [super init])) {
        self.tabBar.hidden =YES;
        
        p_CustomTabbar = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
        [p_CustomTabbar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbarbg.png"]]];
        
        p_TabButtonChoiced = [[NSMutableArray alloc] init];
        p_TabButtonNoneChoiced = [[NSMutableArray alloc] init];
        
        for (int i=0; i<4; i++) {
            UIButton* tTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
            tTabButton.frame =CGRectMake(i*80, 2, 80, 46);
            tTabButton.tag =10+i;
            [tTabButton addTarget:self action:@selector(TabIndexChanged:) forControlEvents:UIControlEventTouchUpInside];
            NSString* tChoicedStr = [NSString stringWithFormat:@"tabbt%d.png",i];
            [p_TabButtonChoiced addObject:[UIImage imageNamed:tChoicedStr]];
            NSString* tNoneChoicedStr = [NSString stringWithFormat:@"ntabbt%d.png",i];
            [p_TabButtonNoneChoiced addObject:[UIImage imageNamed:tNoneChoicedStr]];
            [p_CustomTabbar addSubview:tTabButton];
            if (i == 0) {
                [tTabButton setImage:[p_TabButtonChoiced lastObject] forState:UIControlStateNormal];
            }else{
                [tTabButton setImage:[p_TabButtonNoneChoiced lastObject] forState:UIControlStateNormal];
            }
        }
        
        
        [self.view addSubview:p_CustomTabbar];
        
        ScanViewController* tController0 = [[ScanViewController alloc] init];
        HistoryController* tController1 = [[HistoryController alloc] init];
        ComController* tController2 = [[ComController alloc] init];
        
        LoginController* tController3 = [[LoginController alloc] init];
        
        NSArray* tViewCs= [NSArray arrayWithObjects:tController0,tController1,tController2,tController3,nil];
        [tController0 release];
        [tController1 release];
        [tController2 release];
        [tController3 release];
        self.viewControllers = tViewCs;
    }
    return self;
}

-(void) dealloc{
    [p_TabButtonChoiced release];
    [p_TabButtonNoneChoiced release];
    [p_CustomTabbar release];
    [super dealloc];
}
+(void) setTabbarState:(BOOL) hidden{
    p_CustomTabbar.hidden =hidden;
}

-(void) TabIndexChanged:(UIButton*)sender{
    NSLog(@"index =%d",self.selectedIndex);
    UIButton* tLastButton = (UIButton*)[p_CustomTabbar viewWithTag:10+self.selectedIndex];
    [tLastButton setImage:[p_TabButtonNoneChoiced objectAtIndex:self.selectedIndex] forState:UIControlStateNormal];
    self.selectedIndex = sender.tag-10;
    [sender setImage:[p_TabButtonChoiced objectAtIndex:self.selectedIndex] forState:UIControlStateNormal];
    if (self.selectedIndex == 1) {
        HistoryController* tHis = [self.viewControllers objectAtIndex:1];
        [tHis Fetchdata];
    }else if (self.selectedIndex == 2) {
       
        ComController* tCom = [self.viewControllers objectAtIndex:2];
         tCom.navigationBar.hidden =YES;
        [tCom.p_ComRootController startScan];
    }
}
@end
